package com.dognessnetwork.customer.service.impl;

import com.dognessnetwork.customer.domain.ChatRoom;
import com.dognessnetwork.customer.domain.Comment;
import com.dognessnetwork.customer.repository.ChatRoomRepository;
import com.dognessnetwork.customer.repository.CommentRepository;
import com.dognessnetwork.customer.service.api.CommentService;
import com.dognessnetwork.customer.util.Avgrate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * = CommentServiceImpl TODO Auto-generated class documentation
 *
 */
@Service
public class CommentServiceImpl implements CommentService {
	@Autowired
	private	CommentRepository commentRepository;
	public	CommentRepository	getCommentRepository(){
		return	commentRepository;
	}

    @Autowired
    Avgrate avgrate;

    @Autowired
    ChatRoomRepository chatroomRepository;

    /**
     * TODO Auto-generated method documentation
     * 
     * @param entity
     * @return Comment
     */
    @Transactional
    public Comment save(Comment entity) {
        return getCommentRepository().save(entity);
    }

    @Transactional
    public void scoring(Comment comment) {
        if (getCommentRepository().save(comment) != null) {
            ChatRoom chatRoom = comment.getChatRoom();
            ChatRoom oldChatRoom = chatroomRepository.getOne(chatRoom.getId());
            oldChatRoom.setAvgrate(avgrate.get_avgrate(chatRoom));
            chatroomRepository.save(oldChatRoom);
        }
    };
	/**
     * TODO Auto-generated method documentation
     * 
     * @param chatRoom
     * @param star
     * @param pageable
     * @return Page
     */
    public Page<Comment> findByChatRoomAndStar(ChatRoom chatRoom, Integer star, Pageable pageable) {
        return getCommentRepository().findByChatRoomAndStar(chatRoom, star, pageable);
    }
}
